﻿using AntdUI;
using ImportPlcPointPosition.Forms;
using ImportPlcPointPosition.Models.Dto;
using ImportPlcPointPosition.Models.ViewModels;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZR.Model.Models.ProductionControl;

namespace ImportPlcPointPosition.Controls
{
    public partial class AlarmDictPage : BaseUserControl
    {
        BindingList<CksAlarmDict> Source = new BindingList<CksAlarmDict>();
        PagerInfo pager = new PagerInfo(1,50);
        public AlarmDictPage()
        {
            InitializeComponent();
            InitTable();
            pagination1.PageSize = 50;
            pagination1.PageSizeOptions = new int[] { 50, 100 };
            pagination1.ShowSizeChanger = true;
            pagination1.InitData(pager.PageNum,pager.PageSize);
            pagination1.ValueChanged += Pagination1_ValueChanged;
            pagination1.ShowTotalChanged += Pagination1_ShowTotalChanged;
            pagination1.MaxPageTotal = 10;
            pagination1.RightToLeft = RightToLeft.Yes;
            InitData();
        }

        private string Pagination1_ShowTotalChanged(object sender, PagePageEventArgs e)
        {
            return $"总计:{e.Total}条";
        }

        private void InitTable()
        {
            tbDict.EditMode = TEditMode.None;
            tbDict.Columns = new ColumnCollection();
            tbDict.Columns.Add(new Column("PlcId", "PlcId"));
            tbDict.Columns.Add(new Column("BlockDb", "BlockDb"));
            tbDict.Columns.Add(new Column("AlarmCode", "报警代码")); // 
            tbDict.Columns.Add(new Column("BitId", "位编号"));
            tbDict.Columns.Add(new Column("SignalAddress", "地址"));
            tbDict.Columns.Add(new Column("DeviceName", "设备名称"));
           
            tbDict.Columns.Add(new Column("FaultCase", "故障原因"));
            tbDict.Columns.Add(new Column("Solution", "解决方法"));

            tbDict.Bordered = true;
            tbDict.EnableHeaderResizing = true;
            tbDict.EmptyHeader = true;
            tbDict.Gap = 6;
            tbDict.VisibleHeader = true;


            tbDict.CellBeginEditInputStyle += (s, e) =>
            {
                e.Input.SelectAll();
            };
            //table1.CellClick += Table1_CellClick;

        }


        private void InitData()
        {
            var db = SqlSugarHelper.MesDB;
            var lst = db.Queryable<PlcConfig>().ToList();
            List<SelectOption> plcItems = new List<SelectOption>();
            foreach (var item in lst)
            {
                plcItems.Add(new SelectOption() { Text = item.WorkstationCode, Source = item.Id });
            }
            selectPlc.Items.AddRange(plcItems.ToArray());

            selectPlc.MouseLeave += SelectPlc_MouseLeave;
        }

        private void SelectPlc_MouseLeave(object sender, EventArgs e)
        {
            var item = selectPlc.SelectedValue;
            if (item != null)
            {
                selectPlc.Text = (item as SelectOption).Text;
            }
        }



        private void btnImportExcel_Click(object sender, EventArgs e)
        {
            var res = new ImportAlarmDictForm().ShowDialog();
            if (res == DialogResult.OK)
            {
                MessageInfo("已导入OK");
                pager.PageNum = 1;
                ReflushData();
            }
        }

        public override void ReflushPage()
        {

            btnSearch_Click(null,null);
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            pager.PageNum = 1;
            ReflushData();
        }

        private void Pagination1_ValueChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            pager.PageNum = e.Current;
            pager.PageSize = e.PageSize;

            ReflushData();
        }
        private void ReflushData()
        {
            var item1 = selectPlc.SelectedValue;
            var queryPlcId = 0;
            if(item1 != null)
            {
                queryPlcId = (item1 as SelectOption).Source.ObjToInt();
            }
            int dbNumber = txtDBNumber.Text.ObjToInt();
            string searchContent = txtSearch.Text.Trim();
            var db = SqlSugarHelper.MesDB;
            Source.Clear();
            Stopwatch stopwatch = Stopwatch.StartNew();
            var total = 0;
            var lst = db.Queryable<CksAlarmDict>()
                    .WhereIF( queryPlcId>0 ,a=>a.PlcId == queryPlcId )
                     .WhereIF(dbNumber > 0, a => a.BlockDb == dbNumber)
                    .WhereIF( !searchContent.IsNullOrEmpty(),a=>a.FaultCase.Contains(searchContent) )
                   .OrderBy(a => new {a.PlcId,a.BlockDb ,a.AlarmId }).ToPageList(pager.PageNum,pager.PageSize,ref total);
            pager.TotalNum = total;
            pagination1.PageSize = pager.PageSize;
            pagination1.Total = total;
           
            stopwatch.Stop();

            MessageInfo($"查询耗时{stopwatch.ElapsedMilliseconds}ms");
            Source = new BindingList<CksAlarmDict>(lst);
            tbDict.Binding(Source);
        }

        
    }
}
